<?php

class GuestbookCtrl extends Layoutable {
	public function __construct() {
		parent::__construct();
		$this->title('Guestbook');
	}
	public function actIndex() {
		$fd = $this->_getFd();
		$this->rows = array();
		while($row = fgets($fd)) {
			$this->rows[]=json_decode($row);
		}
		$this->signal()->emit('beforeGuestbookIndex', $this->rows);
	}
	public function actAdd($args) {
		if($msg=@$args['msg']) {
			$this->signal()->emit('BeforeGuestBookAdd', $args);
			$fd = $this->_getFd();
			fputs($fd, json_encode(array('time'=>time(),'msg'=>$msg,'ip'=>$_SERVER['REMOTE_ADDR'])));
			fputs($fd, "\n");
			fclose($fd);
			$url = $this->url(array('ctrl'=>'Guestbook', 'act'=>'Index'));
			$this->http()->redirect($url);
		}		
		$this->title('Add');		
	}
	protected function _getFd() {
		$conf = $this->config();
		$fd = fopen($conf['datafile'], 'a+');
		return $fd;
	}
}
